<template>
  <div>
    <transition name="confirm">
      <div v-show="visible" class="confirm">
        <div class="confirm-inner">
          <div class="title">{{ title }}</div>
          <div class="actions">
            <div class="cancel-text" @click="cancel">{{ cancelText }}</div>
            <div class="confirm-text" @click="confirm">{{ confirmText }}</div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Confirm',
  props: {
    title: {
      required: true,
      type: String
    },
    confirmText: {
      type: String,
      default: '确定'
    },
    cancelText: {
      type: String,
      default: '取消'
    }
  },
  data() {
    return {
      visible: false,
      confirmClicked: false,
      cancelClicked: false
    }
  },
  methods: {
    hide() {
      this.visible = false
    },

    confirm() {
      this.hide()
      this.confirmClicked = true
    },

    cancel() {
      this.hide()
      this.cancelClicked = true
    }
  }
}
</script>

<style lang="scss" scoped>
.confirm {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 998;
  .confirm-inner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
    width: 250px;
    border-radius: 14px;
    background-color: #fff;
    .title {
      text-align: center;
      padding: 20px 15px;
      line-height: 22px;
      font-size: 16px;
    }
    .actions {
      display: flex;
      align-items: center;
      text-align: center;
      font-size: 16px;
      > div {
        flex: 1;
        padding: 10px 0;
        line-height: 22px;
        border-top: 1px solid rgba(0, 0, 0, 0.3);
        &.cancel-text {
          border-right: 1px solid rgba(0, 0, 0, 0.3);
        }
      }
    }
  }
}

.confirm-enter-from,
.confirm-leave-to {
  opacity: 0;
  .confirm-inner {
    transform: scale(0);
  }
}

.confirm-enter-active {
  animation: opacity-fadeIn 0.3s linear;
  .confirm-inner {
    animation: size-fadeIn 0.3s linear;
  }
}

.confirm-leave-active {
  animation: opacity-fadeOut 0.3s linear;
  .confirm-inner {
    animation: size-fadeOut 0.3s linear;
  }
}

@keyframes opacity-fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes opacity-fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@keyframes size-fadeIn {
  0% {
    transform: translate3d(-50%, -50%, 0) scale(0);
  }
  50% {
    transform: translate3d(-50%, -50%, 0) scale(1.1);
  }
  100% {
    transform: translate3d(-50%, -50%, 0) scale(1);
  }
}

@keyframes size-fadeOut {
  0% {
    transform: translate3d(-50%, -50%, 0) scale(1);
  }
  50% {
    transform: translate3d(-50%, -50%, 0) scale(1.1);
  }
  100% {
    transform: translate3d(-50%, -50%, 0) scale(0);
  }
}
</style>
